import Vue from 'vue'
import Router from 'vue-router'
import Login from "../views/Login";
import Main from "../views/Main";
import {Message} from "element-ui";
import ChangePassword from "../views/user/ChangePassword";
import MainMsg from "../views/MainMsg";
import UserMsgDetail from "../views/user/UserMsgDetail";
import PingBanSale from "../views/sale/PingBanSale";
import PingBanBuy from "../views/buy/PingBanBuy"
Vue.use(Router)

const  router=new Router({
  mode: "history",
  routes: [
    {
      path:"/",
      redirect:'/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/main',
      name: 'Main',
      component: Main,
      meta:{
        isLogin:true
      },
      children:[
        {
          path:'/change-person',
          name:'ChangePerson',
          component:ChangePerson,
          meta:{
            isLogin:true
          }
        },
        {
          path:'/change-password',
          name:'ChangePassword',
          component:ChangePassword,
          meta:{
            isLogin:true
          }
        },
        {
          path:'/main',
          name:'MainMsg',
          component:MainMsg,
          meta:{
            isLogin: true
          }
        },
        {
            path:'/user-msg-detail',
            name:'UserMsgDetail',
            component:UserMsgDetail,
            meta:{
              isLogin: true
            }
        },

        // 销售 and 客户 -----------------
        {
          path:'/ping-ban-sale',
          name:'PingBanSale',
          component:PingBanSale,
          meta:{
            isLogin: true,
            isPower: true,
            power: 2,
            department:'销售部',
          }
        },
        {
          path:'/juan-ban-sale',
          name:'JuanBanSale',
          component:JuanBanSale,
          meta:{
            isLogin: true,
            isPower: true,
            power: 2,
            department:'销售部',
          }
        },
        {
          path:'/other-products-sale',
          name:'OtherProductsSale',
          component:OtherProductsSale,
          meta:{
            isLogin: true,
            isPower: true,
            power: 2,
            department:'销售部',
          }
        },
        {
          path:'/client-group-msg',
          name:'ClientGroupMsg',
          component:ClientGroupMsg,
          meta:{
            isLogin: true,
            isPower: true,
            power: 2,
            department:'销售部',
          }
        },
        {
          path:'/client-manage',
          name:'ClientManage',
          component:ClientManage,
          meta:{
            isLogin: true,
            isPower: true,
            power: 2,
            department:'销售部',
          }
        },

        {
          path:'/client-regtered',
          name:'ClientRegistered',
          component:ClientRegistered,
          meta:{
            isLogin: true,
            isPower: true,
            power: 2,
            department:'销售部',
          }
        },
        {
          path:'/find-sale-recording',
          name:'FindSaleRecording',
          component:FindSaleRecording,
          meta:{
            isLogin: true,
            isPower: true,
            power: 2,
            department:'销售部',
          }
        },

        // 开平---------------------------
        {
          path:'/kai-ping-msg',
          name:'KaiPingMsg',
          component:KaipingMsg,
          meta:{
            isLogin: true,
            isPower: true,
            power: 3,
            department:'开平部',
          }
        },
        {
          path:'/kai-ping-order/:inventoryId/:smallTypeId/:quantity',
          name:'KaiPingOrder',
          component:KaiPingOrder,
          meta:{
            isLogin: true,
            isPower: true,
            power: 3,
            department:'开平部',
          }
        },
        {
          path:'/find-kaiping-recording',
          name:'FindKaiPingRecording',
          component:FindKaiPingRecording,
          meta:{
            isLogin: true,
            isPower: true,
            power: 3,
            department:'开平部',
          }
        },
        {
          path:'/kaipingguy-manager',
          name:'KaiPingGuyManage',
          component:KaiPingGuyManage,
          meta:{
            isLogin: true,
            isPower: true,
            power: 3,
            department:'开平部',
          }
        },
        {
          path:'/kaiping-comparison',
          name:'KaiPingComparison',
          component:KaiPingComparison,
          meta:{
            isLogin: true,
            isPower: true,
            power: 3,
            department:'开平部',
          }
        },

        // 采购 ------------------------
        {
          path:'/ping-ban-buy',
          name:'PingBanBuy',
          component:PingBanBuy,
          meta:{
            isLogin: true,
            isPower: true,
            power: 4,
            department:'采购部',
          }
        },
        {
          path:'/find-buy-recording',
          name:'FindBuyRecording',
          component:FindBuyRecording,
          meta:{
            isLogin: true,
            isPower: true,
            power: 4,
            department:'采购部',
          }
        },
        {
          path:'/juan-ban-buy',
          name:'JuanBanBuy',
          component:JuanBanBuy,
          meta:{
            isLogin: true,
            isPower: true,
            power: 4,
            department:'采购部',
          },
        },
        {
          path:'/other-products-buy',
          name:'OtherProductsBuy',
          component:OtherProductsBuy,
          meta:{
            isLogin: true,
            isPower: true,
            power: 4,
            department:'采购部',
          },
        },
        {
          path:'/suppliers-manage',
          name:'SuppliersManage',
          component:SuppliersManage,
          meta:{
            isLogin: true,
            isPower: true,
            power: 4,
            department:'采购部',
          },
        },


        // 库存-----------------
        {
          path:'/inventory-recording',
          name:'InventoryRecording',
          component:InventoryRecording,
          meta:{
            isLogin: true,
          },
        },
        {
          path:'/inventory-recording-by-daigang',
          name:'InventoryRecordingByDaiGang',
          component:InventoryRecordingByDaiGang,
          meta:{
            isLogin: true,
          },
        },
        {
          path:'/inventory-recording-by-pingban',
          name:'InventoryRecordingByPingBan',
          component:InventoryRecordingByPingBan,
          meta:{
            isLogin: true,
          },
        },
      //  系统(办公室,开发人员)------------------
        {
          path:'/product-bigtype-show',
          name:'ProductBigTypeShow',
          component:ProductBigTypeShow,
          meta:{
            isLogin: true,
            isPower: true,
            power: 1,
            department:'办公部',
          },
        },
        {
          path:'/product-smalltype-show',
          name:'ProductSmallTypeShow',
          component:ProductSmallTypeShow,
          meta:{
            isLogin: true,
            isPower: true,
            power: 1,
            department:'办公部',
          },
        },
        {
          path:'/warehouse-show',
          name:'WarehouseShow',
          component:WarehouseShow,
          meta:{
            isLogin: true,
            isPower: true,
            power: 1,
            department:'办公部',
          },
        },
        {
          path:'/basic-data-change',
          name:'BasicDataChange',
          component:BasicDataChange,
          meta:{
            isLogin: true,
            isPower: true,
            power: 1,
            department:'办公部',
          },
        },
        {
          path:'/unit-show',
          name:'UnitShow',
          component:UnitShow,
          meta:{
            isLogin: true,
            isPower: true,
            power: 1,
            department:'办公部',
          },
        },
        {
          path:'/department-show',
          name:'DepartmentShow',
          component:DepartmentShow,
          meta:{
            isLogin: true,
            isPower: true,
            power: 1,
            department:'办公部',
          },
        },
        {
          path:'/employee-show',
          name:'EmployeeShow',
          component:EmployeeShow,
          meta:{
            isLogin: true,
            isPower: true,
            power: 1,
            department:'办公部',
          },
        },
      //  账务 ------------------
        {
          path:'/company-account',
          name:'CompanyAccount',
          component:CompanyAccount,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/account-big-type',
          name:'AccountBigType',
          component:AccountBigType,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/account-small-type',
          name:'AccountSmallType',
          component:AccountSmallType,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/add-account',
          name:'AddAccount',
          component:AddAccount,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/find-account-recording',
          name:'FindAccountRecording',
          component:FindAccountRecording,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/find-invoice-recording',
          name:'FindInvoiceRecording',
          component:FindInvoiceRecording,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/client-tiaopiao-and-reconciliation',
          name:'ClientTiaoPiaoAndReconciliation',
          component:ClientTiaoPiaoAndReconciliation,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/suppliers-tiaopiao-and-reconciliation',
          name:'SuppliersTiaoPiaoAndReconciliation',
          component:SuppliersTiaoPiaoAndReconciliation,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/kaipingguy-tiaopiao-and-reconciliation',
          name:'KaiPingGuyTiaoPiaoAndReconciliation',
          component:KaiPingGuyTiaoPiaoAndReconciliation,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/find-reconciliation-recording',
          name:'FindReconciliationRecording',
          component:FindReconciliationRecording,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },
        {
          path:'/find-tiaopiao-recording',
          name:'FindTiaoPiaoRecording',
          component:FindTiaoPiaoRecording,
          meta:{
            isLogin: true,
            isPower: true,
            power: 5,
            department:'账务部',
          },
        },

      ]
    }

  ]
})

import {getPower} from "../utils/power";
import {get} from "../utils/request";
import JuanBanBuy from "../views/buy/JuanBanBuy";
import OtherProductsBuy from "../views/buy/OtherProductsBuy";
import SuppliersManage from "../views/buy/SuppliersManage";
import JuanBanSale from "../views/sale/JuanBanSale";
import OtherProductsSale from "../views/sale/OtherProductsSale";
import FindBuyRecording from "../views/buy/FindBuyRecording";
import FindSaleRecording from "../views/sale/FindSaleRecording";
import KaipingMsg from "../views/kaiping/KaiPingMsg";
import KaiPingOrder from "../views/kaiping/KaiPingOrder";
import FindKaiPingRecording from "../views/kaiping/FindKaiPingRecording";
import KaiPingGuyManage from "../views/kaiping/KaiPingGuyManage";
import InventoryRecording from "../views/inventory/InventoryRecording";
import InventoryRecordingByDaiGang from "../views/inventory/InventoryRecordingByDaiGang";
import InventoryRecordingByPingBan from "../views/inventory/InventoryRecordingByPingBan";
import ClientGroupMsg from "../views/client/ClientGroupMsg";
import ClientManage from "../views/client/ClientManage";
import ClientRegistered from "../views/client/ClientRegistered";
import CompanyAccount from "../views/account/CompanyAccount";
import AccountBigType from "../views/account/AccountBigType";
import AccountSmallType from "../views/account/AccountSmallType";
import AddAccount from "../views/account/AddAccount";
import  ProductBigTypeShow from "../views/system/ProductBigTypeShow"
import ProductSmallTypeShow from "../views/system/ProductSmallTypeShow";
import WarehouseShow from "../views/system/WarehouseShow";
import KaiPingComparison from "../views/kaiping/KaiPingComparison";
import UnitShow from "../views/system/UnitShow";
import DepartmentShow from "../views/system/DepartmentShow";
import EmployeeShow from "../views/system/EmployeeShow";
import FindAccountRecording from "../views/account/FindAccountRecording";
import FindInvoiceRecording from "../views/account/FindInvoiceRecording";
import ClientTiaoPiaoAndReconciliation from "../views/account/ClientTiaoPiaoAndReconciliation";
import SuppliersTiaoPiaoAndReconciliation from "../views/account/SuppliersTiaoPiaoAndReconciliation";
import KaiPingGuyTiaoPiaoAndReconciliation from "../views/account/KaiPingGuyTiaoPiaoAndReconciliation";
import FindReconciliationRecording from "../views/account/FindReconciliationRecording";
import FindTiaoPiaoRecording from "../views/account/FindTiaoPiaoRecording";
import ChangePerson from "../views/user/ChangePerson";
import BasicDataChange from "../views/system/BasicDataChange";

router.beforeEach((to, from, next)=>{


     if(to.meta.isLogin){

       if (localStorage.getItem("token")){
         get('/api/v1/token/test2').then(resp=>{

         })

         if (to.meta.isPower){
          getPower().then(resp=>{
            localStorage.removeItem("department")
            localStorage.setItem("department",resp.data)
            let power= localStorage.getItem("department")
            var realPower=Number(power)
            if (realPower===to.meta.power||realPower===1){
              next()
            }else {
              Message.error("抱歉,你并没有"+to.meta.department+"的权限,如需要请跟管理员联系")
            }
           })

         }else {
           next()
         }
       }else {
        Message.error("你还未登录,请先登录")
         next({
           path: '/'
         })
       }
     }else {
       next()
     }
})


export  default  router
